/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asfabdesk.presistencia;

import asfabdesk.apresentacao.TelaLogin;
import asfabdesk.dominio.Associado;
import asfabdesk.dominio.CategoriaVinculo;
import asfabdesk.dominio.CentroDeCusto;
import asfabdesk.dominio.CidadeUf;
import asfabdesk.dominio.ContribuicaoMensal;
import asfabdesk.dominio.Convenio;
import asfabdesk.dominio.ConvenioPorAssociado;
import asfabdesk.dominio.ConvenioPorDependente;
import asfabdesk.dominio.Dependente;
import asfabdesk.dominio.DepositoFinanceiro;
import asfabdesk.dominio.Documento;
import asfabdesk.dominio.EmpresaConveniada;
import asfabdesk.dominio.EntradaFinanceira;
import asfabdesk.dominio.EntradaTransferencia;
import asfabdesk.dominio.HistoricoFinanceiro;
import asfabdesk.dominio.ItemEntrada;
import asfabdesk.dominio.ItemProcesso;
import asfabdesk.dominio.ItemSaida;
import asfabdesk.dominio.NivelAcesso;
import asfabdesk.dominio.Observacao;
import asfabdesk.dominio.Processo;
import asfabdesk.dominio.SaidaFinanceira;
import asfabdesk.dominio.Secretaria;
import asfabdesk.dominio.Tag;
import asfabdesk.dominio.TipoDocumento;
import asfabdesk.dominio.TipoEntrada;
import asfabdesk.dominio.TipoSaida;
import asfabdesk.dominio.TipoServico;
import asfabdesk.dominio.SaidaTransferencia;
import asfabdesk.dominio.Usuario;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Marco Aurélio
 * 
 */
public class HibernateUtil {

    private static final Configuration configuracao;
    private static final SessionFactory fabrica;
    private static String ip =TelaLogin.ip;
    private static String porta = TelaLogin.porta;
    private static String banco = TelaLogin.banco;

    static {
        AnnotationConfiguration config = new AnnotationConfiguration();
        mapearEntidades(config);        
        configurarHibernate(config);
        configurarC3PO(config);
        configuracao = config;
        fabrica = configuracao.buildSessionFactory();
    }

    private static void mapearEntidades(AnnotationConfiguration config) {
        config.addAnnotatedClass(Associado.class);
        config.addAnnotatedClass(CategoriaVinculo.class);
        config.addAnnotatedClass(CidadeUf.class);
        config.addAnnotatedClass(CentroDeCusto.class);
        config.addAnnotatedClass(ContribuicaoMensal.class);
        config.addAnnotatedClass(Convenio.class);
        config.addAnnotatedClass(ConvenioPorAssociado.class);
        config.addAnnotatedClass(ConvenioPorDependente.class);
        config.addAnnotatedClass(Dependente.class);
        config.addAnnotatedClass(DepositoFinanceiro.class);
        config.addAnnotatedClass(Documento.class);
        config.addAnnotatedClass(ItemEntrada.class);
        config.addAnnotatedClass(EmpresaConveniada.class);
        config.addAnnotatedClass(EntradaFinanceira.class);
        config.addAnnotatedClass(HistoricoFinanceiro.class);
        config.addAnnotatedClass(ItemProcesso.class);
        config.addAnnotatedClass(ItemSaida.class);
        config.addAnnotatedClass(NivelAcesso.class);
        config.addAnnotatedClass(Observacao.class);
        config.addAnnotatedClass(Processo.class);
        config.addAnnotatedClass(SaidaFinanceira.class);
        config.addAnnotatedClass(Secretaria.class);
        config.addAnnotatedClass(Tag.class);
        config.addAnnotatedClass(TipoDocumento.class);
        config.addAnnotatedClass(TipoEntrada.class);
        config.addAnnotatedClass(TipoSaida.class);
        config.addAnnotatedClass(TipoServico.class);        
        config.addAnnotatedClass(Usuario.class);

    }

    private static void configurarHibernate(AnnotationConfiguration config) {
        config.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        config.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
        config.setProperty("hibernate.connection.username", "asfab");
        config.setProperty("hibernate.connection.password", "cazaquistao02");
        config.setProperty("hibernate.connection.url", "jdbc:postgresql://" + ip + ":" + porta +"/"+banco);
        config.setProperty("hibernate.show_sql", "false");
        config.setProperty("hibernate.default_schema", "public");
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }

    private static void configurarC3PO(AnnotationConfiguration config) {
        config.setProperty("c3p0.min_size", "10");
        config.setProperty("c3p0.max_size", "40");
        config.setProperty("c3p0.timeout", "180");
        config.setProperty("c3p0.idle_test_period", "100");
    }

    public static SessionFactory getFabrica() {
        return fabrica;
    }

    public static Session getSession() {
        return fabrica.openSession();
    }

    public static Configuration getConfiguracao() {
        return configuracao;
    }
}
